/* Copyright - Apache License 2.0
 * 
 * The project "kyou" is
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *      http://www.apache.org/licenses/LICENSE-2.0
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package net.kyou.util;

import java.io.UnsupportedEncodingException;
import java.nio.charset.Charset;

import net.kyou.exception.KyouErr;
import net.kyou.exception.KyouException;

import org.junit.Assert;
import org.junit.Test;

public class KyouStringTest {
    @Test
    public void before() {
        new KyouString();
    }

    @Test
    public void testParse() throws UnsupportedEncodingException {
        try {
            byte[] bytes = KyouString.parse("aaa", Charset.forName("utf-8"));
            Assert.assertArrayEquals("aaa".getBytes("utf-8"), bytes);
        } finally {
        }

        try {
            byte[] bytes = KyouString.parse("[]{}", Charset.forName("gb2312"));
            Assert.assertArrayEquals("[]{}".getBytes("gb2312"), bytes);
        } finally {
        }

        try {
            byte[] bytes = KyouString.parse("\\\\", Charset.forName("utf-8"));
            Assert.assertArrayEquals("\\".getBytes("utf-8"), bytes);
        } finally {
        }

        try {
            byte[] bytes = KyouString.parse("aa\\\\bb", Charset.forName("utf-8"));
            Assert.assertArrayEquals("aa\\bb".getBytes("utf-8"), bytes);
        } finally {
        }

        try {
            byte[] bytes = KyouString.parse("\\01", Charset.forName("utf-8"));
            Assert.assertArrayEquals(new byte[] { 1 }, bytes);
        } finally {
        }

        try {
            byte[] bytes = KyouString.parse("\\0A\\0B\\0C\\0D", Charset.forName("utf-8"));
            Assert.assertArrayEquals(new byte[] { 0x0a, 0x0b, 0x0c, 0x0d }, bytes);
        } finally {
        }
        try {
            byte[] bytes = KyouString.parse("工要在地一上是中国同", Charset.forName("iso8859-1"));
            Assert.assertArrayEquals("工要在地一上是中国同".getBytes("iso8859-1"), bytes);
        } finally {
        }
    }

    @Test
    public void testParseEx() {
        try {
            KyouString.parse(null, KyouRuntimeUtils.utf8);
            Assert.fail();
        } catch (KyouException ex) {
            Assert.assertEquals(KyouErr.Style.KyouStringSyntaxError, ex.err);
        }

        try {
            KyouString.parse("asdf", null);
            Assert.fail();
        } catch (KyouException ex) {
            Assert.assertEquals(KyouErr.Base.UnsupportedCharset, ex.err);
        }

        try {
            KyouString.parse("asdf\\", KyouRuntimeUtils.utf8);
            Assert.fail();
        } catch (KyouException ex) {
            Assert.assertEquals(KyouErr.Style.KyouStringSyntaxError, ex.err);
        }

        try {
            KyouString.parse("asdf\\0", KyouRuntimeUtils.utf8);
            Assert.fail();
        } catch (KyouException ex) {
            Assert.assertEquals(KyouErr.Style.KyouStringSyntaxError, ex.err);
        }

        try {
            KyouString.parse("asdf\\\\\\", KyouRuntimeUtils.utf8);
            Assert.fail();
        } catch (KyouException ex) {
            Assert.assertEquals(KyouErr.Style.KyouStringSyntaxError, ex.err);
        }

        try {
            KyouString.parse("asdf\\a\\b\\c\\d\\e", KyouRuntimeUtils.utf8);
            Assert.fail();
        } catch (KyouException ex) {
            Assert.assertEquals(KyouErr.Style.KyouStringSyntaxError, ex.err);
        }

        try {
            KyouString.parse("asdf\\KK", KyouRuntimeUtils.utf8);
            Assert.fail();
        } catch (KyouException ex) {
            Assert.assertEquals(KyouErr.Style.KyouHexStringSyntaxError, ex.err);
        }
    }

}
